<?php

require_once('Zend/Db/Table.php');
require_once('Zend/Db/Table/Row/Abstract.php');

class Core_Page
	extends Zend_Db_Table_Row_Abstract
{
    /**
     * Name of the class of the Zend_Db_Table_Abstract object.
     *
     * @var string
     */
    protected $_tableClass = 'Zend_Db_Table';
    
	/**
	 * Private static reference to the user table.
	 * 
     * @return Zend_Db_Table
	 */
	private static $table;

    /**
     * Select access method, for slightly more complex queries.
     *
     * @return Zend_Db_Table_Select
     */
    public static function getTableInstance()
    {
    	if ( empty(Core_Page::$table ) )
    	{
    		Core_Page::$table = new Zend_Db_Table('page');
    		Core_Page::$table->setRowClass('Core_Page');
    	}
    	
    	return Core_Page::$table;
    }
    
    /**
     * Factory Method. Spits out a page object that's tied to the database.
     *
     * @return Core_Page
     */
    public static function factory( $id_or_name = null )
    {
    	$table = Core_Page::getTableInstance();
    	
    	if ( is_null($id_or_name) )
    	{
    		$row = $table->createRow(array(), Zend_Db_Table_Abstract::DEFAULT_DB);
    	}
    	else if ( is_int($id_or_name) )
    	{
    		$row = $table->fetchRow( $table->select()->where('pageId=?',$id_or_name));
    	}
    	else
    	{
    		$row = $table->fetchRow( $table->select()->where('name=?',$id_or_name));
    	}
    	
    	return $row;
    }
	
	/**
	 * @inheritDoc
	 */
    protected function _doInsert()
    {
    	$this->created = new Zend_Db_Expr('NOW()');
    	$this->updated = new Zend_Db_Expr('NOW()');
    	
    	parent::_doInsert();
    }
    
	/**
	 * @inheritDoc
	 */
    protected function _doUpdate()
    {
    	$this->updated = new Zend_Db_Expr('NOW()');
    	
    	parent::_doUpdate();
    }
}